Iterated Fact Sheets With R Markdown

Iterated Fact Sheets With R Markdown

Aaron R. Williams (@awunderground)

Urban Institute

Urban Institute



Who we are

  • 300 researchers
  • 50 years

What we do

Our mission is to open minds, shape decisions, and offer solutions through economic and social policy research.

Our objective research helps expand opportunities for all, reduce hardship among the most vulnerable, and strengthen the effectiveness of the public sector.

  • Tax Policy Center analysis of candidate tax plans
  • Health Policy Center analysis of insurance coverage (King vs. Burwell)
  • Pioneers of paired testing and microsimulation

Why?

Motivation

Getting information into the right hands is important.

Motivation

Our reports often seem like this.

Motivation

Our attention spans are trained for this.

## [1] "https://imgs.xkcd.com/comics/boyfriend.png"

Economic and social policy comic strips

Economic and social policy comic strips Iterated fact sheets and web pages

What does this look like?

1. Targeted and brief

What does this look like?

2. Visually appealing (and branded)

Urban Institute data visualization style guide

What does this look like?

3. Not labor intensive

cat

What?

Examples

How?

R Markdown

.Rmd

  • Text and images
  • Code
  • Code output

.Rmd

  • Text and images
  • Code
  • Code output


R4DS

Three ingredients for fact sheets/pages

  1. Data structure
  2. Parameterized .Rmd template
  3. Iterating script

1) Data structure

state_name hhpop horate medhhincome
Alabama 1846380 0.6814329 44700
Alaska 250183 0.6311860 70600
Arizona 2463012 0.6206178 51000
Arkansas 1144657 0.6546031 42000
California 12895471 0.5372219 64600
Colorado 2074517 0.6389425 63500
Connecticut 1343704 0.6623386 71700
Delaware 352597 0.7096147 61200
  • Typically one row per fact sheet/page

2) Parameterized .Rmd template

Parameters for R Markdown documents are specified in the YAML header:

---
params:
  state: "Alabama"
  year: 2016
---

params$state can be used anywhere in the document as "Alabama".

First, use the parameters to filter the big data structure to just data for an individual state or locality.

2) Parameterized .Rmd template

---
params:
  state: "Alabama"
  year: 2016
---

Now, we can use data$<variable> for the state/locality specific variables and params$<parameter> throughout the document:


There were 'r data$fatalities' traffic fatalities in 'r params$state' in 'r params$year'. 

There were 1,038 traffic fatalities in Alabama in 2016.

2) Parameterized .Rmd template

Data viz!

2) Parameterized .Rmd template

  • LaTeX for .pdf
  • preamble.tex
  • general document styles
  • LaTeX macros
    • urbntitle{}
    • urbnboilerplate{}

2) Parameterized .Rmd template

  • CSS for .html
  • CSS for .md
  • styles.css

3) Iterating script


3) Iterating script

TIP! Don’t use NA in a YAML header.

---
params:
  state: "Alabama"
  year: 2016
---

Fact Sheet Tool

Fact Page Tool

Overall Process

  1. Why fact sheets?
  2. Create a Word/Google Doc template (if collaborating)
  3. Recreate the template for one iteration in .Rmd
  4. Iterate the .Rmd
  5. Quality control
  6. Create a home for the fact sheets

Takeaways

  • Visually consistent
  • Accurate
  • Easy to update

Questions

Reach out to me

References

References

NHTSA Traffic Safety Fact Sheets

Texas Statehouse - LoneStarMike

Encyclopedia - WestportWiki